package com.net.xpay.clean.scheduler;


import com.beust.jcommander.internal.Lists;
import com.net.common.util.LocalDateUtil;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.UserShard;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.manager.UserShardManager;
import com.net.xpay.core.domain.PayTransBatch;
import com.net.xpay.core.domain.safePay.SafePayBatch;
import com.net.xpay.core.enums.PayTransBatchStatus;
import com.net.xpay.core.enums.SafePayBatchStatus;
import com.net.xpay.core.manager.PayTransBatchManager;
import com.net.xpay.core.manager.safePay.SafePayBatchManager;
import com.net.xpay.core.model.PayTransBatchSearchModel;
import com.net.xpay.core.model.SafePayBatchSearchModel;
import com.net.xpay.core.service.PayTransBatchService;
import com.net.xpay.core.service.safePay.SafePayInnerService;
import com.net.shard.ShardUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 2019/7/15 16:28

 * 修复或者初始化数据
 */
@Slf4j
//@Component
public class UpdateBenefitScheduler {

    @Autowired
    private UserManager userManager;

    @Autowired
    private UserShardManager userShardManager;

    @Autowired
    private SafePayBatchManager safePayBatchManager;

    @Autowired
    private SafePayInnerService safePayInnerService;

    @Autowired
    private PayTransBatchManager payTransBatchManager;

    @Autowired
    private PayTransBatchService payTransBatchService;



    /**
     * 跟新二月1号到现在的数据
     */
//    @Scheduled(cron = "0 40 18 * * ?")
    public void cleanSafePayBatch() {
        log.info("start cleanSafePayBatch");
        List<User> userList = userManager.listAll();
        long count = 0L;
        LocalDateTime maxEndTime = LocalDateUtil.parseStringToDateTime("2024-02-27 17:30:00");
        for (User user : userList) {
            if (user.getPartnerId() == null) {
                continue;
            }
            UserShard userShard = userShardManager.getById(user.getId());
            try {
                ShardUtil.setShardContext(userShard.getShardId());
                LocalDateTime minEndTime = LocalDateUtil.parseStringToDateTime("2024-02-01 00:00:00");
                while (minEndTime.isBefore(maxEndTime)) {
                    SafePayBatchSearchModel searchModel = new SafePayBatchSearchModel();
                    searchModel.setUserId(user.getId());
                    searchModel.setMinEndTime(minEndTime);
                    LocalDateTime dayEnd = LocalDateUtil.getDayEnd(minEndTime);
                    if (dayEnd.getDayOfMonth() == 27) {
                        dayEnd = maxEndTime;
                    }
                    searchModel.setMaxEndTime(dayEnd);
                    searchModel.setStatusList(Lists.newArrayList(SafePayBatchStatus.SUCCESS, SafePayBatchStatus.PART_SUCCESS));
                    List<Long> ids = safePayBatchManager.listIds(searchModel);
                    for (Long id : ids) {
                        SafePayBatch safePayBatch = safePayBatchManager.getById(id);
                        safePayInnerService.calBenefit(safePayBatch, user);
                        safePayBatchManager.updateByPrimaryKeyWithVersion(safePayBatch);
                        count++;
                    }

                    minEndTime = minEndTime.plusDays(1L);
                }
            } finally {
                ShardUtil.cleanShardContext();
            }
            log.info("cleanSafePayBatch user finished userid={}", user.getId());

        }

        log.info("end cleanSafePayBatch|count={}", count);
    }

//    @Scheduled(cron = "0 49 19 * * ?")
    public void cleanPayTransBatch() {
        log.info("start cleanPayTransBatch");
        List<User> userList = userManager.listAll();
        long count = 0L;
        LocalDateTime maxEndTime = LocalDateUtil.parseStringToDateTime("2024-02-27 17:30:00");
        for (User user : userList) {
            if (user.getPartnerId() == null) {
                continue;
            }
            UserShard userShard = userShardManager.getById(user.getId());
            try {
                ShardUtil.setShardContext(userShard.getShardId());
                LocalDateTime minEndTime = LocalDateUtil.parseStringToDateTime("2024-02-01 00:00:00");
                while (minEndTime.isBefore(maxEndTime)) {
                    PayTransBatchSearchModel searchModel = new PayTransBatchSearchModel();
                    searchModel.setUserId(user.getId());
                    searchModel.setMinEndTime(minEndTime);
                    LocalDateTime dayEnd = LocalDateUtil.getDayEnd(minEndTime);
                    if (dayEnd.getDayOfMonth() == 27) {
                        dayEnd = maxEndTime;
                    }
                    searchModel.setMaxEndTime(dayEnd);
                    searchModel.setStatusList(Lists.newArrayList(PayTransBatchStatus.SUCCESS, PayTransBatchStatus.PART_SUCCESS));
                    List<Long> ids = payTransBatchManager.listIds(searchModel);
                    for (Long id : ids) {
                        PayTransBatch payTransBatch = payTransBatchManager.getById(id);
                        payTransBatchService.calBenefit(payTransBatch, user, payTransBatch.getRefund());
                        payTransBatchManager.updateByPrimaryKeyWithVersion(payTransBatch);
                        count++;
                    }

                    minEndTime = minEndTime.plusDays(1L);
                }
            } finally {
                ShardUtil.cleanShardContext();
            }
            log.info("cleanPayTransBatch user finished userid={}", user.getId());

        }

        log.info("end cleanPayTransBatch|count={}", count);

    }




}
